<?php

/**
 * Pager 클래스 
 * 
 * @package php.util 
 */

/**
 * 리스트에서 paging을 쉽게 해주는 클래스입니다.
 *
 * @package php.util
 */
class Pager extends PObject {
	private $page;
	private $block;
	private $limitPage;
	private $limitBlock;
	Private $totalCount;
	private $totalPage ;
	private $totalBlock;
	private $startPage;
	private $endPage;
	private $location; 
	private $title = array();

	public function __construct($url, $page, $limitPage, $limitBlock, $totalCount) {
		$this->location		= $url ;
		$this->page			= $page;
		$this->limitPage	= $limitPage;
		$this->limitBlock	= $limitBlock;
		$this->totalCount	= $totalCount;

		// 현재 블럭 계산하기 
		$this->totalPage	= ceil($this->totalCount / $this->limitPage);
		$this->block		= ceil($this->page/$this->limitBlock);
		$this->totalBlock	= ceil($this->totalPage / $this->limitBlock);

		// 현재 블럭의 시작페이지와 마지막페이지 설정
		$this->startPage	= ($this->block - 1) * $this->limitBlock + 1;
		$this->endPage		= $this->startPage + $this->limitBlock - 1;

		if ($this->endPage > $this->totalPage) {
			$this->endPage = $this->totalPage ;
		}

		$this->setTitle(array('[처음]','[이전]','[다음]','[끝]'));
	}

	public function setTitle($arr) {
		$this->title = $arr;
	}

	public function getPage()
	{
		return $this->page;
	}

	// 페이지 번호 변경
	public function replaceStr($i) {
		return str_replace("%page%", $i, $this->location);
	}

	// 전체 페이지 수 얻어오기 
	public function getTotalPage() {
		return $this->totalPage;
	}

	// 처음 페이지 
	public function getFirstPage() {
		return ($this->page > 1) ? "1" : "";
	}

	//이전 블럭
	public function getPrevBlock() {
		return ($this->block > 1) ? ($this->startPage - $this->limitBlock) : "";
	}

	// 시작 페이지
	public function getStartPage() {
		return $this->startPage;
	}

	// 마지막 페이지
	public function getEndPage() {
		return $this->endPage;
	}

	// 페이지 시작 번호 
	public function getStartNum() {
		return $this->totalCount - ($this->limitPage * ($this->page - 1));
	}

	// 다음 블럭
	public function getNextBlock() {
		return ($this->block < $this->totalBlock) ? ($this->endPage+1) : "";
	}

	// 마지막 페이지 
	public function getLastPage() {
		return ($this->page < $this->totalPage) ? ($this->totalPage) : "";
	}

	public function toString() {
		$str = "";

		if ($this->getFirstPage() != "") {
			$str .= "<a href='".$this->replaceStr($this->getFirstPage())."'>".$this->title[0]."</a>";
		} else {
			$str .= $this->title[0];
		}

		$str .= "&nbsp;";

		if ($this->getPrevBlock() != "") {
			$str .= "<a href='".$this->replaceStr($this->getPrevBlock())."'>".$this->title[1]."</a>";
		} else {
			$str .= $this->title[1];
		}

		$str .= "&nbsp;";
		
		if ($this->getTotalPage() <= 1) {
			$str .= " <strong>1</strong> ";
		} else {

			for ($i = $this->startPage; $i <= $this->endPage; $i++) {
				if ($i == $this->page) {
					$str .= "<strong>".$i."</strong>&nbsp;";
				} else {
					$str .= "<a href='".$this->replaceStr($i)."'>".$i."</a>&nbsp;";
				}
			}
		}

		$str .= "&nbsp;";		

		if ($this->getNextBlock() != "") {
			$str .= "<a href='".$this->replaceStr($this->getNextBlock())."'>".$this->title[2]."</a>";
		} else {
			$str .= $this->title[2];
		}

		$str .= "&nbsp;";

		if ($this->getLastPage() != "") {
			$str .= "<a href='".$this->replaceStr($this->getLastPage())."'>".$this->title[3]."</a>";
		} else {
			$str .= $this->title[3];
		}

		return $str;
	}
}

?>